
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_my_web/bean/request/text_request.dart';
import 'package:flutter_my_web/event/change_root_event.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/net/BaseResponse.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/BlueMaterialButton.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
class SaveEditarticleBean{
  var id = "";
  var isEdit = false;
  var typeStr = "";
  var saveContent = "";
  var titleStr = "";
  var descStr = "";
  var fileTagStr = "";
  var coverStr = "";

}
void startSaveEditarticleDialogWidget(SaveEditarticleBean bean) {
  Get.dialog(SaveEditarticleDialogWidget(bean)).then((value) => Get.back());
}

class SaveEditarticleDialogWidget extends StatefulWidget {
  SaveEditarticleBean bean;


  SaveEditarticleDialogWidget(this.bean);

  @override
  _SaveEditarticleDialogWidgetState createState() =>
      _SaveEditarticleDialogWidgetState();
}

class _SaveEditarticleDialogWidgetState
    extends State<SaveEditarticleDialogWidget> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      content: Container(
        padding: EdgeInsets.zero,
        child: Material(
          color: Colors.white,
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                SimpleText("${widget.bean.isEdit?"编辑":"保存"}MD文件"),
                TextFormField(
                  // enabled: !widget.bean.isEdit,
                  controller: TextEditingController(text: widget.bean.titleStr),
                  onChanged: (change){
                    widget.bean.titleStr = change;
                  },
                  decoration: InputDecoration(
                      labelText: "文件名",
                      hintText: "请输入文件名称",
                      icon: Icon(Icons.file_copy)),
                  maxLength: 200,
                ),
                TextFormField(
                  // enabled: !widget.bean.isEdit,
                  controller: TextEditingController(text: widget.bean.descStr),
                  onChanged: (change){
                    widget.bean.descStr = change;
                  },
                  decoration: InputDecoration(
                      labelText: "描述",
                      hintText: "请输入描述",
                      icon: Icon(Icons.title)),
                  maxLength: 200,
                ),
                TextFormField(
                  // enabled: !widget.bean.isEdit,
                  controller: TextEditingController(text: widget.bean.fileTagStr),
                  onChanged: (change){
                    widget.bean.fileTagStr = change;
                  },
                  decoration: InputDecoration(
                      labelText: "标签",
                      hintText: "请输入标签,逗号隔开",
                      icon: Icon(Icons.tag_faces)),
                  maxLength: 200,
                ),
                TextFormField(
                  // enabled: !widget.bean.isEdit,
                  controller: TextEditingController(text: widget.bean.coverStr),
                  onChanged: (change){
                    widget.bean.coverStr = change;
                  },
                  decoration: InputDecoration(
                      labelText: "封面",
                      hintText: "请输入封面",
                      icon: Icon(Icons.content_paste_outlined)),
                  maxLength: 200,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    BlueMaterialButton("取消", () {
                      Get.back();
                    }),
                    BlueMaterialButton("保存", () async {
                       saveFile();
                    }),
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future<void> saveFile() async {
    if (widget.bean.titleStr.isEmpty){
      showSnackBar("请输入文件名");
      return;
    }
    var request = AddTextRequest();
    request.id = widget.bean.id;
    request.title = widget.bean.titleStr;
    request.desc = widget.bean.descStr;
    request.tags = widget.bean.fileTagStr;
    request.coverUrl = widget.bean.coverStr;
    request.type = widget.bean.typeStr==""?"md":widget.bean.typeStr;
    request.content = widget.bean.saveContent;
    print("desc:${widget.bean.descStr}");
    BaseResponse response;
    // if(widget.bean.isEdit){
    //   response= await FileManagerApi().updateText(request);
    // }else{
    //   response =await FileManagerApi().addText(request);
    // }
    // LogUtils.logger.d("response :${response.status}");
    // if(response.status=="1"){
    //   Get.back();
    //   showSnackBar("保存成功");
    //   LogUtils.logger.d("保存成功");
    //   refreshHome();
    // }else{
    //   showSnackBar("保存失败：" + response.errmessage!);
    // }


  }

}
